package com.baizhi.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQUtil {
    //成员变量
    private static ConnectionFactory factory = null;


    static {
        //创建一个连接工厂
        factory = new ConnectionFactory();

        //设置服务器IP地址
        factory.setHost("192.168.154.129");

        //rabbitMQ端口
        factory.setPort(5672);

        //操作哪个虚拟主机
        factory.setVirtualHost("rabbit");
        factory.setHandshakeTimeout(50000);

        //用户名
        factory.setUsername("tj");

        //密码
        factory.setPassword("2001823");

    }
    public static Connection getConnection(){

        Connection connection = null;
        //获取连接
        try {
            connection = factory.newConnection();

        }catch (Exception e){
            e.printStackTrace();
        }


        return connection;
    }

    public static void close(Channel channel,Connection connection){
        try {
            if(channel != null)channel.close();
            if(connection != null)connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
